package com.hbean.service.impl;

import com.hbean.entity.SysRole;
import com.hbean.mapper.SysRoleMapper;
import com.hbean.service.SysRoleService;
import com.hbean.system.config.SysCache;
import com.hbean.utils.Condition;
import com.hbean.utils.HUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: null
 * @Project: hbean
 * @Author: hjibo
 * @CreateDate: 2018-10-27 22:06
 * @ModificationHistory:
 */

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysCache sysCache;

    @Override
    @CachePut(value = "sysCache", key="'sysRole'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysRole> insertDataAndUpdateCache(SysRole sysRole) {

        sysRole.setSr03(sysRole.getSr02());//父级节点编号
        sysRole.setSr04(sysCache.getSysRoleNameByID(sysRole.getSr02()));
        sysRole.setSr00(HUtil.uuidOf18());//主键
        sysRole.setSr02(HUtil.uuidOf18());//唯一标识

        this.sysRoleMapper.insertData(sysRole);

        return this.sysRoleMapper.selectDatas(new Condition());
    }

    @Override
    @CachePut(value = "sysCache", key="'sysRole'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysRole> deleteDataAndUpdateCache(SysRole sysRole) {

        this.sysRoleMapper.deleteData(sysRole);

        return this.sysRoleMapper.selectDatas(new Condition());
    }

    @Override
    @CachePut(value = "sysCache", key="'sysRole'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysRole> updateDataAndUpdateCache(SysRole sysRole) {

        this.sysRoleMapper.updateData(sysRole);

        return this.sysRoleMapper.selectDatas(new Condition());
    }

    @Override
    @Cacheable(value = "sysCache", key="'sysRole'") //开启缓存策略
    public List<SysRole> getDataForCache() {
        return this.sysRoleMapper.selectDatas(new Condition());
    }
}
